package com.hzvdvmc.autosale.setting.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.hzvdvmc.autosale.R;

import java.util.List;

/**
 * Created by william on 2018/1/13.
 */

public class SimpleTextSpinnerAdatper extends BaseAdapter implements SpinnerAdapter {

    private List<String> dataList;

    public SimpleTextSpinnerAdatper(List<String> dataList) {
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList == null ? null : dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        ViewHodler hodler = null;
        if (convertView == null) {
            hodler = new ViewHodler();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_spinner_drow, null);
            hodler.mTextView = (TextView) convertView.findViewById(R.id.tv_item_spinner);
            convertView.setTag(hodler);
        } else {
            hodler = (ViewHodler) convertView.getTag();
        }
        hodler.mTextView.setText(dataList.get(position));
        return convertView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler hodler = null;
        if (convertView == null) {
            hodler = new ViewHodler();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_spinner, null);
            hodler.mTextView = (TextView) convertView.findViewById(R.id.tv_item_spinner);
            convertView.setTag(hodler);
        } else {
            hodler = (ViewHodler) convertView.getTag();
        }
        hodler.mTextView.setText(dataList.get(position));
        return convertView;
    }

    private static class ViewHodler {
        TextView mTextView;
    }
}
